<template>
  <div>
    <h2>编辑器材</h2>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item to="/AdminMain/AdminHome">主页</el-breadcrumb-item>
      <el-breadcrumb-item to="/AdminMain/EquipmentManager">器材管理</el-breadcrumb-item>
      <el-breadcrumb-item>编辑器材</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card style="margin-top: 20px;">
      <el-form :model="equipment" :rules="rules" ref="editForm" label-width="100px">
        <el-form-item label="器材ID">
          <el-input v-model="equipment.equipmentId" disabled />
        </el-form-item>
        <el-form-item label="器材名称" prop="equipmentName">
          <el-input v-model="equipment.equipmentName" clearable />
        </el-form-item>
        <el-form-item label="器材位置" prop="equipmentLocation">
          <el-input v-model="equipment.equipmentLocation" clearable />
        </el-form-item>
        <el-form-item label="器材状态" prop="equipmentStatus">
          <el-select v-model="equipment.equipmentStatus" placeholder="请选择器材状态" clearable>
            <el-option label="正常" value="正常" />
            <el-option label="维修中" value="维修中" />
            <el-option label="已报废" value="已报废" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注信息">
          <el-input v-model="equipment.equipmentMessage" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">确认修改</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import axios from 'axios'

const router = useRouter()
const route = useRoute()

const equipment = ref({
  equipmentId: '',
  equipmentName: '',
  equipmentLocation: '',
  equipmentStatus: '',
  equipmentMessage: ''
})

const editForm = ref(null)
const rules = {
  equipmentName: [{ required: true, message: '请输入器材名称', trigger: 'blur' }],
  equipmentLocation: [{ required: true, message: '请输入器材位置', trigger: 'blur' }],
  equipmentStatus: [{ required: true, message: '请选择器材状态', trigger: 'change' }]
}

const loadEquipment = (id) => {
  axios.get('/server/equipment/findById', { params: { equipmentId: id } })
      .then(response => {
        if (response.data.code === 200) {
          equipment.value = response.data.data
        } else {
          ElMessage.error('获取器材数据失败：' + response.data.message)
        }
      })
      .catch(() => {
        ElMessage.error('请求出错，无法获取器材信息')
      })
}

watch(() => route.query.equipmentId, (newId) => {
  if (newId) loadEquipment(newId)
})

onMounted(() => {
  const id = route.query.equipmentId
  if (!id) {
    ElMessage.error('缺少器材ID')
    router.push('/AdminMain/EquipmentManager')
    return
  }
  loadEquipment(id)
})

const onSubmit = () => {
  editForm.value.validate((valid) => {
    if (valid) {
      axios.put('/server/equipment/update', equipment.value)
          .then(response => {
            if (response.data.code === 200) {
              ElMessage.success('修改成功')
              setTimeout(() => router.push('/AdminMain/EquipmentManager'), 600)
            } else {
              ElMessage.error('修改失败：' + response.data.message)
            }
          })
          .catch(() => {
            ElMessage.error('提交失败，请检查网络或联系管理员')
          })
    }
  })
}
</script>
